package cn.edu.xhu.library_management.department.service.impl;

import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import cn.edu.xhu.library_management.department.entity.LmClass;
import cn.edu.xhu.library_management.department.entity.LmFaculty;
import cn.edu.xhu.library_management.department.entity.LmMajor;
import cn.edu.xhu.library_management.department.entity.search.SearchMajor;
import cn.edu.xhu.library_management.department.mapper.ClassMapper;
import cn.edu.xhu.library_management.department.mapper.FacultyMapper;
import cn.edu.xhu.library_management.department.mapper.MajorMapper;
import cn.edu.xhu.library_management.department.service.MajorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022-01-29
 * @Description: 专业接口实现类
 */
@Service
public class MajorServiceImpl implements MajorService {
    @Resource
    private MajorMapper majorMapper;
    @Resource
    private FacultyMapper facultyMapper;
    @Resource
    private ClassMapper classMapper;
    @Override
    public boolean addMajor(LmMajor major) {
        addUpComRule(major);
        major.setName(major.getName().trim());
        return majorMapper.addMajor(major) > 0;
    }

    @Override
    public boolean updateMajor(LmMajor major) {
        addUpComRule(major);
        return majorMapper.updateMajor(major) > 0;
    }

    @Override
    public boolean deleteMajorById(Integer maId) {
        // 查找绑定的班级是否清空
        List<LmClass> classes = classMapper.findClassesByMajor(maId);
        if (!classes.isEmpty())
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"请先删除专业对应的班级");
        return majorMapper.deleteMajorById(maId) > 0;
    }

    @Override
    public boolean deleteMajorByIds(List<Integer> maIds) {
        for (Integer maId : maIds) {
            List<LmClass> lmClasses = classMapper.findClassesByMajor(maId);
            if (!lmClasses.isEmpty()) {
                throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"请先删除专业对应的班级");
            }
        }
        return majorMapper.deleteMajorByIds(maIds) > 0;
    }

    @Override
    public List<LmMajor> findAllMajor() {
        return majorMapper.findAllMajor();
    }

    @Override
    public List<LmMajor> findMajorsByDepartment(Integer faId) {
        return majorMapper.findMajorsByDepartment(faId);
    }

    @Override
    public LmMajor findMajorById(Integer maId) {
        return majorMapper.findMajorById(maId);
    }

    @Override
    public Map<String, Object> search(SearchMajor searchMajor) {
        // 如果没有分页则不设置
        if (searchMajor.getCurrentPage() != null && searchMajor.getPageSize() != null){
            searchMajor.setCurrentPage((searchMajor.getCurrentPage() -1)*searchMajor.getPageSize());
        }

        List<LmMajor> majors = majorMapper.search(searchMajor);
        // 获取总条数
        Integer total = majorMapper.searchTotal(searchMajor);
        Map<String, Object> map = new HashMap<>();
        map.put("majors",majors);
        map.put("total",total);
        return map;
    }

    /**
     * 添加修改公共验证方法
     * @param major 专业对象
     */
    private void addUpComRule(LmMajor major ){
        // 通过名称查找专业是否存在
        LmMajor fMajor = majorMapper.findMajorByName(major.getName());
        if (fMajor != null)
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"专业名称已存在");
        // 查找院系ID是否符合
        LmFaculty faculty = facultyMapper.findFacultyById(major.getFaId());
        if (faculty == null)
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"院系不存在");
    }
}
